﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using AcctrueWMS.Foundation;
using System.Text.RegularExpressions;
using System.Collections;

namespace AcctrueWMS.Foundation.Web.Modules
{
    public class ModuleManager
    {
        private ModuleMenuTop moduleMenu;

        public ModuleMenuTop ModuleMenu
        {
            get { return moduleMenu; }
            set { moduleMenu = value; }
        }

        public void LoadModules(string filePath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);

            moduleMenu = TravelModule(xmlDoc.DocumentElement);
        }

        //遍历xml，加载功能模块
        private ModuleMenuTop TravelModule(XmlNode moduleNode)
        {
            #region old code
            //ModuleInfo module = new ModuleInfo();
            //if (moduleNode.Name == "modules")
            //{
            //    module.Childs = new System.Collections.ObjectModel.Collection<ModuleInfo>();
            //    foreach (XmlNode childNode in moduleNode.SelectNodes("child/module"))
            //    {
            //        module.Childs.Add(TravelModule(childNode));
            //    }
            //    return module;
            //}
            //module.ModuleName = moduleNode.Attributes["name"].Value;
            //module.WBSCode = moduleNode.Attributes["wbsCode"].Value;
            //module.Url = GetAttrValueIfEmpity(moduleNode.Attributes, "url", "");
            //module.ShowInMenu = bool.Parse(GetAttrValueIfEmpity(moduleNode.Attributes, "showInMenu", "false"));
            //if (module.Actions == null)
            //    module.Actions = new System.Collections.ObjectModel.Collection<ModuleAction>();
            //if (moduleNode.SelectNodes("actions").Count > 0)
            //{

            //    foreach (XmlNode actionNode in moduleNode.SelectSingleNode("actions").SelectNodes("action"))
            //    {
            //        ModuleAction action = new ModuleAction();
            //        action.ActionName = actionNode.Attributes["name"].Value;
            //        action.ControlID = GetAttrValueIfEmpity(actionNode.Attributes, "controlID", "");
            //        action.MaskNo = int.Parse(actionNode.Attributes["maskNo"].Value);
            //        module.Actions.Add(action);
            //    }
            //}
            //if (module.Childs == null)
            //    module.Childs = new System.Collections.ObjectModel.Collection<ModuleInfo>();
            //if (moduleNode.SelectNodes("child").Count > 0)
            //{

            //    foreach (XmlNode childNode in moduleNode.SelectSingleNode("child").SelectNodes("module"))
            //    {

            //        module.Childs.Add(TravelModule(childNode));
            //    }
            //}
            //return module;

            #endregion

            //读取module.xml的菜单项目到 modulemenu
            ModuleMenuTop moduleMenu = GetModuleMenuTopFromXml(moduleNode);

            return moduleMenu;

        }

        public ModuleInfo GetModuleByUrl(string url)
        {
            for (int i = 0; i < moduleMenu.ModulesLevelOne.Count; i++)
            {
                for (int j = 0; j < moduleMenu.ModulesLevelOne[i].Modules.Count; j++)
                {
                    ////int startIndex = url.IndexOf("model.aspx");
                    ////url = url.Substring(startIndex, url.Length - startIndex);\
                    ////此处根据url 的 model 与 temp 进行比较
                    //if (CompareArrlist(GetModelTempByUrl(moduleMenu.ModulesLevelOne[i].Modules[j].Url), GetModelTempByUrl(url)))
                    //{
                    //    return moduleMenu.ModulesLevelOne[i].Modules[j];
                    //}

                    //int index = url.IndexOf(moduleMenu.ModulesLevelOne[i].Modules[j].Url);
                    string model = GetValueFromUrl(moduleMenu.ModulesLevelOne[i].Modules[j].Url,"model");
                    string temp = GetValueFromUrl(moduleMenu.ModulesLevelOne[i].Modules[j].Url,"temp");

                    
                    string model1 = GetValueFromUrl(url,"model");
                    string temp1 = GetValueFromUrl(url,"temp");

                    if ((temp == temp1) && (model == model1))
                        return moduleMenu.ModulesLevelOne[i].Modules[j];

                    //int index1 = url.IndexOf(url);
                    //if (index >= 0)
                    //{
                    //    return moduleMenu.ModulesLevelOne[i].Modules[j];
                    //}
                }

            }
            return null;
        }
        public bool CompareArrlist(ArrayList comp1, ArrayList comp2)
        {
            bool result = true;
            if (comp1.Count != comp2.Count)
                result = false;
            bool[] bList = new bool[comp1.Count];
            for (int i = 0; i < comp1.Count; i++)
            {
                if (comp1[i].ToString() == comp2[i].ToString())
                    bList[i] = true;
                else
                    bList[i] = false;
            }
            for (int i = 0; i < bList.Length; i++)
            {
                if (bList[i] == false)
                    result = false;
            }
            return result;
        }
        private string GetValueFromUrl(string url,string key)
        {
            Regex modelTemp_reg = new Regex(@"(?<name>[^=&?]*)?=(?<value>[^&]*)"); //new Regex(@"{(?<colname>[\w|\d]+)}\s*");
            ArrayList modelTemp = new ArrayList();
            foreach (Match m in modelTemp_reg.Matches(url))
            {
                
                string name = m.Groups["name"].Value;
                string value = m.Groups["value"].Value;

                if (name == key)//other parameter can add
                    return value;
            }
            return "";
        }

        private void Fill(ModuleMenuTop moduleMenu, XmlNode moduleNode)
        {

            throw new NotImplementedException();
        }

        private ModuleMenuTop GetModuleMenuTopFromXml(XmlNode moduleNode)
        {
            ModuleMenuTop moduleMenuTopTemp = new ModuleMenuTop();


            foreach (XmlNode childNode in moduleNode.SelectNodes("child/module"))
            {
                ModuleLevelOne moduleLevelOneTemp = new ModuleLevelOne();
                moduleLevelOneTemp.ModuleName = childNode.Attributes["name"].Value;
                moduleLevelOneTemp.WBSCode = childNode.Attributes["name"].Value;
                if (!(childNode.Attributes["showInMenu"].Value == null))
                    moduleLevelOneTemp.ShowInMenu = bool.Parse(childNode.Attributes["showInMenu"].Value);
                moduleLevelOneTemp.Parent = childNode.Attributes["parent"].Value;
                moduleLevelOneTemp.DisplayName = childNode.Attributes["text"].Value;

                foreach (XmlNode childNode1 in childNode.SelectNodes("child/module"))
                {
                    ModuleInfo moduleInfoTemp = new ModuleInfo();
                    moduleInfoTemp.ModuleName = childNode1.Attributes["name"].Value;
                    moduleInfoTemp.Url = childNode1.Attributes["url"].Value;

                    if (childNode1.Attributes["limitUrl"] != null && childNode1.Attributes["limitUrl"].Value != string.Empty)
                        moduleInfoTemp.Url = childNode1.Attributes["limitUrl"].Value;

                    moduleInfoTemp.ShowInMenu = bool.Parse(childNode1.Attributes["showInMenu"].Value);
                    moduleInfoTemp.Parent = childNode1.Attributes["parent"].Value;
                    moduleInfoTemp.DisplayName = childNode1.Attributes["text"].Value;
                    moduleInfoTemp.WBSCode = childNode1.Attributes["name"].Value;
                    moduleInfoTemp.DefaultPrivilege = childNode1.Attributes["defaultPrivilege"] != null ? childNode1.Attributes["defaultPrivilege"].Value : "none";

                    foreach (XmlNode childNode2 in childNode1.SelectNodes("actions/action"))
                    {
                        ModuleAction moduleActionTemp = new ModuleAction();

                        moduleActionTemp.ActionName = childNode2.Attributes["name"].Value;
                        moduleActionTemp.ControlID = childNode2.Attributes["controlID"].Value;
                        moduleActionTemp.MaskNo = int.Parse(childNode2.Attributes["maskNo"].Value);

                        moduleInfoTemp.Childs.Add(moduleActionTemp.ControlID, moduleActionTemp);

                    }
                    moduleLevelOneTemp.Modules.Add(moduleInfoTemp.ModuleName, moduleInfoTemp);
                }
                moduleMenuTopTemp.ModulesLevelOne.Add(moduleLevelOneTemp.ModuleName, moduleLevelOneTemp);
            }


            return moduleMenuTopTemp;
        }


        #region oldCode

        //private ModuleInfo rootModule;
        //private string GetAttrValueIfEmpity(XmlAttributeCollection attrs, string name, string substitute)
        //{
        //    return (attrs[name] == null) ? substitute : attrs[name].Value;
        //}

        //public ModuleInfo GetRootModule()
        //{
        //    return rootModule;
        //}
        ////递归寻找模块
        //private ModuleInfo RecusiveFind(ModuleInfo module, string name, string wbsCode)
        //{
        //    if (!string.IsNullOrEmpty(wbsCode))
        //    {
        //        if (module.WBSCode == wbsCode)
        //            return module;
        //    }
        //    else if (!string.IsNullOrEmpty(name))
        //    {
        //        if (module.ModuleName == name)
        //            return module;
        //    }
        //    //else
        //    //{
        //    foreach (ModuleInfo child in module.Childs)
        //    {
        //        ModuleInfo ret;
        //        if ((ret = RecusiveFind(child, name, wbsCode)) != null)
        //            return ret;
        //    }
        //    // }
        //    return null;
        //}
        //public ModuleInfo GetModuleInfoByName(string name)
        //{
        //    return RecusiveFind(rootModule, name, null);
        //}

        //public ModuleInfo GetModuleInfoByWBS(string wbs)
        //{
        //    return RecusiveFind(rootModule, null, wbs);
        //}
        #endregion
    }
}
